import storage from '@/util/storage'
import api from '@/api'
import {Message} from 'element-ui'
import {currentDate, diffTime} from '@/util/date'

const state = {
    account: storage.local.get('account') || '',
    token: storage.local.get('token') || '',
    failure_time: storage.local.get('failure_time') || '',
    permissions: [],
    info: JSON.parse(storage.local.get('user_info')) || {},
    message: 0
}

const getters = {
    isLogin: state => {
        const endTime = currentDate.date
        const startTime = storage.local.get('start_time')
        const diff = diffTime(startTime, endTime)
        let retn = false
        if (state.token) {
            // let unix = Date.parse(new Date())
            // if (unix < state.failure_time * 1000) {
            //     retn = true
            // }
            // if (diff > 7) {
            //     storage.local.remove('start_time')
            //     Message.error('登录过期，请重新登录！')
            //     return retn
            // }
            retn = true
        }
        return retn
    }
}

const actions = {
    login({commit}, data) {
        return new Promise((resolve, reject) => {
            // 通过 mock 进行登录
            // api.post('mock/member/login', data).then(res => {
            //     console.log(data)
            //     const userInfo = {
            //         avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
            //         name: '李明',
            //         tag: '教师'
            //     }
            //     commit('setUserData', res.data)
            //     commit('setUserInfo', userInfo)
            //     resolve()
            // }).catch(error => {
            //     reject(error)
            // })
            api.post('/user/login', data).then(res => {
                const {code, error} = res.data
                if (code !== 200) {
                    Message.error(error)
                    return reject(error)
                }
                const userInfo = {
                    avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
                    name: '李明',
                    tag: '教师'
                }
                commit('setUserData', res.data)
                commit('setUserInfo', userInfo)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
    logout({commit}) {
        commit('removeUserData')
        commit('menu/invalidRoutes', null, {root: true})
        commit('tabbar/clean', null, {root: true})
    },
    getUserMessage({commit}) {
        commit('setUserMessage', 0)
    },
    // 获取我的权限
    getPermissions({state, commit}) {
        return new Promise(resolve => {
            // 通过 mock 获取权限
            api.get('/user/permission', {
                params: {
                    account: state.account
                }
            }).then(res => {
                commit('setPermissions', res.data.permissions)
                resolve(res.data.permissions)
            })
        })
    }
}

const mutations = {
    setUserData(state, data) {
        storage.local.set('account', data.account)
        storage.local.set('token', data.token)
        storage.local.set('failure_time', data.failure_time)
        state.account = data.account
        state.token = data.token
        state.failure_time = data.failure_time
    },
    removeUserData(state) {
        storage.local.remove('account')
        storage.local.remove('token')
        storage.local.remove('failure_time')
        storage.local.remove('start_time')
        storage.local.remove('user_info')
        state.account = ''
        state.token = ''
        state.failure_time = ''
    },
    setPermissions(state, permissions) {
        state.permissions = permissions
    },
    setUserInfo(state, data) {
        const setData = {...state.info, ...data}
        storage.local.set('user_info', JSON.stringify(setData))
        storage.local.set('start_time', currentDate.date)
        state.info = setData
    },
    setUserMessage(state, data) {
        state.message = data
    }
}

export default {
    namespaced: true,
    state,
    actions,
    getters,
    mutations
}
